package com.example.shardingsphere.config.shard;

import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;

import java.util.Properties;

/**
 * 根据时间分表配置
 */
public class ShardTableDateTimeConfig {

    /**
     * 表分片算法配置
     * 根据年-月分表
     *
     * @return
     */
    public static ShardingSphereAlgorithmConfiguration shardTableAlgorithmsPros() {

        // 配置分表算法
        Properties tableShardingAlgorithmProps = new Properties();

        tableShardingAlgorithmProps.setProperty("datetime-lower", "2021-08-19 12:12:12");
        // 分片数据源或真实表的后缀格式。会将你的分表的字段时间，转为对应的格式，用以拼在表的后缀
        tableShardingAlgorithmProps.setProperty("sharding-suffix-pattern", "yyyy_M");
        // 分片时间戳的格式
        tableShardingAlgorithmProps.setProperty("datetime-pattern", "yyyy-MM-dd HH:mm:ss");

        return new ShardingSphereAlgorithmConfiguration("INTERVAL", tableShardingAlgorithmProps);
    }
}
